package prepared;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class jdbc {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/yzy_db";
    private static String username = "root";
    private static String password = "123456";
    private static Connection con = null;
    private static Statement stm = null;
    private static ResultSet rs = null;

    public static Connection getCo() throws Exception {
        Class.forName(driver);
        con = DriverManager.getConnection(url, username, password);
        return con;
    }
    public static void close(Connection con, Statement stm, ResultSet rs) throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (stm != null) {
            stm.close();
        }
        if (con != null) {
            con.close();
        }
    }

    public static int update(String sql) throws Exception {
        con = getCo();
        stm = con.createStatement();
        int count = stm.executeUpdate(sql);
        close(con, stm, null);
        return count;
    }

    public static ResultSet query(String sql) throws Exception {
        con = getCo();
        stm = con.createStatement();
        rs = stm.executeQuery(sql);
        close(con, stm, rs);
        return rs;
    }
}